<?php
/*
 *    Prototyp-Ro
 *    Copyright (C) 2011  Patton
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Class User_Register extends Main_Utils{
    function reg_upe($username_reg, $psswd_reg, $psswd_reg_retry, $sex_reg, $email_reg){
        $this->return=array();
	$this->query = sprintf("SELECT `userid` FROM `login` WHERE `userid` = '%s'", mysql_real_escape_string($username_reg));
	$this->result = mysql_query($this->query);

        if (mysql_num_rows($this->result) == 1){
            $this->return[] = _CL_USED_USERNAME;
        }
        if(!preg_match('~^[a-zA-Z0-9]{4,23}$~', $username_reg) || !preg_match('~^[a-zA-Z0-9]{4,23}$~', $psswd_reg)){
            $this->return[] = _CL_ERROR_USERNAME_PASSWORD;
        }
        if (!strcmp($username_reg, $psswd_reg)){
            $this->return[] = _CL_IDENTICAL_USERNAME_PASSWORD;
        }
        if (strcmp($psswd_reg, $psswd_reg_retry)){
            $this->return[] = _CL_DIFFERENT_PASSWORD;
        }
        if(!preg_match('~^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$~', $email_reg)){
            $this->return[]= _CL_INCORRECT_EMAIL;
        }

        if(count($this->return) == 0){
            $this->query = sprintf("INSERT INTO `login` (`id`, `userid`, `user_pass`, `sex`, `email`, `last_ip`) VALUES (NULL, '%s', '%s', '%s', '%s', '%s')", mysql_real_escape_string($username_reg), md5($psswd_reg), intval($sex_reg), mysql_real_escape_string($email_reg), $_SERVER["REMOTE_ADDR"]);
            mysql_query($this->query);
            $this->return[] =  _CL_SUCCESS;
        }
        return Main_Utils::print_return_array($this->return);
    }
}
$User_Register = new User_Register();
?>